'''
Created on 2009-6-12

@author: roamer
'''

import os
import shelve
from Setting import *
from ValueStore import *

class Params:
    def __init__(self):
        self.__fileData = {}
    
    def ReadFile(self):
        file = open(Setting['ParamFile'])
        for line in file.readlines():
            cols = [i for i in line.strip().split(' ') if i != '']
            key = cols[0]
            val = (cols[1],int(cols[2]),int(cols[3]))
            if(self.__fileData.has_key(key)):
                self.__fileData[key].append(val)
            else:
                self.__fileData[key] = [val]
        file.close()
        self.__removeDumplicate()
        
    def SaveResultToFile(self):
        ValueStore.Save(Setting['ParamKey'], self.__fileData)
        
    def RestoreResultFromFile(self):
        self.__fileData = ValueStore.Read(Setting['ParamKey'])
        return  self.__fileData
        
    def __removeDumplicate(self):
        for i in self.__fileData.keys():
            self.__fileData[i] = sorted(self.__fileData[i])
            for j in range(len(self.__fileData[i]) - 2,-1,-1):
                if(self.__fileData[i][j] == self.__fileData[i][j+1]):
                    del self.__fileData[i][j]
